class LoginController < ApplicationController
  def index
    if session[:user_id]
      redirect_to :controller => "land", :action => "index"
    end
  end
  
  def login
    @user = User.new(params[:user])
    logged_in_user = @user.try_to_login
    if logged_in_user
      session[:user_id] = logged_in_user.id
      session[:user] = logged_in_user.name
      flash[:notice] = "登录成功！"
      redirect_to :controller => "land", :action => 'index'
    else
      flash[:notice] = "请检查用户名和密码后重新登录！"
      redirect_to :action => "index"
    end
  end
  
  def logout
    session[:user_id] = nil
    session[:user] = nil
    flash[:notice] = "退出系统！"
    redirect_to :controller => "login", :action => "index"
  end
end
